<?php

namespace Admin\Controller;

class UsersController extends AdminController {

    public function index() {
        $tab = I('tab', 1, 'intval');

        if ($tab == 1) {
            $where['u.user_status'] = 1;
        } elseif ($tab == 2) {
            $where['u.user_status'] = 2;
        } else {
            $where['u.user_status'] = 0;
        }

        $keywords = I('keywords');
        if ($keywords) {
            $map['u.phone'] = array('like', "%{$keywords}%");
            $map['u.real_name'] = array('like', "%{$keywords}%");
            $map['u.company'] = array('like', "%{$keywords}%");
            $map['_logic'] = 'or';
            $where['_complex'] = $map;
        
            $this->assign('keywords', $keywords);
        }

        $M = M()->table("ot_users as u");
        $field = '*';
        $order = "u.user_id desc";
        $list = $this->lists($M, $where, $order, $field);

        $this->assign('tab', $tab);
        $this->assign('_list', $list);
        $this->display();
    }

    public function edit() {
        $user_id = I('user_id', 0, 'intval');
        $res = 0;
        if ($user_id) {
            $status = I('status', 0, 'intval');
            $data['user_status'] = $status;
            if ($status == 2) {
                $data['refuse_desc'] = I('refund_desc');
            }

            $res = M('Users')->where(array('user_id' => $user_id))->save($data);
        }

        if ($res) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

    public function editAccountStatus() {
        $user_id = I('user_id', 0, 'intval');
        $res = 0;
        if ($user_id) {
            $account_status = I('account_status', 0, 'intval');
            $data['account_status'] = $account_status;

            $res = M('Users')->where(array('user_id' => $user_id))->save($data);
        }
        if ($res) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

}
